﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.IO.Ports;

using System.ComponentModel.Composition;

namespace ETRoboCon.TrackingSystem.TakahashiSwitchBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    [Export(typeof(ETRoboConTrakingSystem.ISwitchBoxDevice))]
    [ExportMetadata("DeviceType","Takahashi Switch Box")]
    public partial class MainWindow : Window , ETRoboConTrakingSystem.ISwitchBoxDevice
    {
        public MainWindow()
        {
            InitializeComponent();

            UpdatePortList();

            adapter = new COMPortAdapter();
            adapter.OnSwitchPushed += new COMPortAdapter.SwitchPushedDelegator(adapter_OnSwitchPushed);
        }

        COMPortAdapter adapter = null;

        void UpdatePortList()
        {
            lbCOMPorts.Items.Clear();
            foreach (var port in SerialPort.GetPortNames())
            {
                lbCOMPorts.Items.Add(port);
            }
        }

        private void buttonRefresh_Click(object sender, RoutedEventArgs e)
        {
            UpdatePortList();
        }

        private void buttonConnect_Click(object sender, RoutedEventArgs e)
        {
            if (buttonConnect.Content.ToString().CompareTo("Connect") == 0)
            {
                if (lbCOMPorts.SelectedItem != null)
                {
                    string selected = lbCOMPorts.SelectedItem as string;
                    try
                    {
                        adapter.Open(selected);
                        buttonConnect.Content = "Disconnect";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            else if (buttonConnect.Content.ToString().CompareTo("Disconnect") == 0)
            {
                try
                {
                    adapter.Close();
                    buttonConnect.Content = "Connect";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        void adapter_OnSwitchPushed(ETRoboConTrakingSystem.SwitchBoxButtonType switchId, long tick)
        {
            if (OnButtonStatusChanged != null)
            {
                OnButtonStatusChanged(switchId, tick);
            }
        }


        #region ISwitchBoxDevice Members

        public void Start(ETRoboConTrakingSystem.ISwitchBoxClient client)
        {
            client.OnSwitchBoxLEDStatusChanged += new ETRoboConTrakingSystem.SwitchBoxLEDStatus(client_OnSwitchBoxLEDStatusChanged);
            client.SetDevice(this);
        }

        void client_OnSwitchBoxLEDStatusChanged(bool start, bool reset, bool goalIn, bool retireIn, bool goalOut, bool retierOut)
        {
            if (adapter != null)
            {
                if (adapter.Opened)
                {
                    adapter.UpdateSwitchLightStatus(
                    start: start, reset: reset,
                    goalIn: goalIn, retireIn: retireIn,
                    goalOut: goalOut,
                    retireOut: retierOut);
                }
            }
        }


        public void Stop()
        {
            throw new NotImplementedException();
        }

        public event ETRoboConTrakingSystem.SwitchBoxButtonStatusChanged OnButtonStatusChanged;

        #endregion
    }
}
